// https://www.acwing.com/problem/content/830/

#include <iostream>
#include <vector>

using namespace std;


constexpr int MAX_STACK_SIZE = 100005;

struct EasyStack {
    int nums[MAX_STACK_SIZE];
    int top;
};

EasyStack stk;

void init() {
    stk.top = -1;
}

void push(int x) {
    stk.top++;
    stk.nums[stk.top]=x;
}

void pop() {
    if (stk.top >= 0) {
        stk.top--;
    } else {
        stk.top=-1;
    }
}

int top() {
    return stk.nums[stk.top];
}

bool empty() {
    return stk.top == -1;
}


int main() {
    int n;
    cin>>n;
    string work;
    int x;
    init();
    while (n-->0) {
        cin>>work;
        if (work == "push") {
            cin>>x;
            push(x);
        } else if (work == "pop") {
            pop();
        } else if (work == "empty") {
            if (empty()) {
                cout<<"YES"<<endl;
            } else {
                cout<<"NO"<<endl;
            }
        } else if (work == "query") {
            cout<<top()<<endl;
        }
    }
    return 0;
}
